package cn.sample.moreitems.entity;

import cn.sample.moreitems.event.HandleTickEvent;
import cn.sample.moreitems.init.ModEntities;
import cn.sample.moreitems.init.ModItems;
import cn.sample.moreitems.init.ModParticles;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.projectile.ItemSupplier;
import net.minecraft.world.entity.projectile.ThrowableProjectile;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ThrownRockEntity extends ThrowableProjectile implements ItemSupplier {
    private static final Logger log = LoggerFactory.getLogger(ThrownRockEntity.class);

    public ThrownRockEntity(EntityType<? extends ThrownRockEntity> type, Level level) {
        super(type, level);
    }

    public ThrownRockEntity(Level level, LivingEntity owner) {
        super(ModEntities.THROWN_ROCK.get(), owner, level);
    }


    @Override
    protected void onHit(HitResult result) {
        super.onHit(result);
        // 这里写落地/击中逻辑，例如爆炸：
//         this.level().explode(this, this.getX(), this.getY(), this.getZ(),
//                               0F, Level.ExplosionInteraction.NONE);
        if (!this.level().isClientSide) {
            // 1. 粒子
            ((ServerLevel) this.level()).sendParticles(
                    ModParticles.CUSTOM_EXPLOSION.get(),
                    this.getX(), this.getY(), this.getZ(),
                    1, 0, 0, 0, 0);

            // 中心坐标
            double r = 4.0;
            log.info("{} {} {}", this.getX(), this.getY(), this.getZ());
            AABB box = new AABB(
                    this.getX() - r, this.getY() - 1, this.getZ() - r,
                    this.getX() + r, this.getY() + 1, this.getZ() + r
            );

            // 只要生物
            List<LivingEntity> livings = this.level().getEntities(
                    EntityTypeTest.forClass(LivingEntity.class),
                    box,
                    e -> e instanceof LivingEntity);

            // 只要掉落物
            List<ItemEntity> items = this.level().getEntities(
                    EntityType.ITEM,
                    box,
                    e -> true);
            HandleTickEvent.itemEntityMap.put(new Vec3(this.getX(), this.getY(), this.getZ()), items);
            HandleTickEvent.livingEntityMap.put(new Vec3(this.getX(), this.getY(), this.getZ()), livings);
            discard();
        }
    }

    @Override
    protected void defineSynchedData() { /* 无附加数据 */ }

    @Override
    public Packet<ClientGamePacketListener> getAddEntityPacket() {
        return new ClientboundAddEntityPacket(this);
    }

    @Override
    public @NotNull ItemStack getItem() {
        return new ItemStack(ModItems.THROWN_ROCK.get());
    }
}
